// service/storeData/storeData.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    safeBottom: `30px`,
    cate: [],
    tags: [],
    types: 'edit',
    name: '',
    type: 0,
    credit_code: '',
    license_image: '',
    username: '',
    mobile: '',
    idcard: '',
    front_image: '',
    opposite_image: '',
    abbr: '',
    intro: '',
    logo_image: '',
    category_ids: '',
    goodsNum: 0,
    goods_ids: '',
    leader_name: '',
    leader_mobile: '',
    province: '',
    city: '',
    district: '',
    address: '',
    lng: '',
    lat: '',
    note: '',
    trade_hour: ''
  },
  apply(){
    if (this.data.abbr.trim() === '') return util.toast('请输入商户简称')
    if (this.data.intro.trim() === '') return util.toast('请输入商户简介')
      if (this.data.logo_image.trim() === '') return util.toast('请上传店铺Logo')
      if (this.data.category_ids.trim() === '') return util.toast('请选择店铺经营范围')
      if (this.data.goods_ids.trim() === '') return util.toast('请选择服务项目')
      if (this.data.leader_name.trim() === '') return util.toast('请输入负责人姓名')
      if (this.data.leader_mobile.trim() === '') return util.toast('请输入负责人手机号')
      if (this.data.lat === '') return util.toast('请选择地址')
      if (this.data.address === '') return util.toast('请输入详细地址')
      http.post('shop/apply', {
        types: this.data.types,
        name: this.data.name,
        type: this.data.type,
        credit_code: this.data.credit_code,
        license_image: this.data.license_image,
        username: this.data.username,
        idcard: this.data.idcard,
        mobile: this.data.mobile,
        front_image: this.data.front_image,
        opposite_image: this.data.opposite_image,
        abbr: this.data.abbr,
        trade_hour: this.data.trade_hour,
        intro: this.data.intro,
        logo_image: this.data.logo_image,
        category_ids: this.data.category_ids,
        goods_ids: this.data.goods_ids,
        leader_name: this.data.leader_name,
        leader_mobile: this.data.leader_mobile,
        province: this.data.province,
        city: this.data.city,
        district: this.data.district,
        address: this.data.address,
        lng: this.data.lng,
        lat: this.data.lat,
      }, true).then(res => {
        util.toast(res.data)
        this.setData({
          state: 0
        })
      })
  },



  chooseAddress(){
    http.chooseLocation(this.data.lat,this.data.lng).then(res =>{
      if(this.data.province != res.province || this.data.city != res.city || this.data.district != res.district){
        this.setData({
          goods_ids: '',
          goodsNum: 0,
        })
      }
      this.setData({
        info: res.province+res.city+res.district,
        province: res.province,
        city: res.city,
        district: res.district,
        address: res.name,
        lng: res.lng,
        lat: res.lat,
      })
    })
  },
  setAddress(){
    this.setData({
      address: e.detail.value
    })
  },
  setLeader(e) {
    this.setData({
      leader_name: e.detail.value
    })
  },
  setLmobile(e) {
    this.setData({
      leader_mobile: e.detail.value
    })
  },
  setHours(e) {
    this.setData({
      trade_hour: e.detail.value
    })
  },


  


  setAbbr(e) {
    this.setData({
      abbr: e.detail.value
    })
  },
  
  setIntro(e) {
    this.setData({
      intro: e.detail.value
    })
  },
  chooseLogo() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        logo_image: res.data.url
      })
    })
  },

  pushtags(e){
    if(this.data.tags.includes(e.currentTarget.dataset.id)) {
     this.data.tags = this.data.tags.filter(item => item != e.currentTarget.dataset.id)
    } else {
      this.data.tags.push(e.currentTarget.dataset.id)
    }
    this.setData({
      tags:this.data.tags,
      category_ids: this.data.tags.join(','),
      goods_ids: '',
      goodsNum: 0,
    })
  },
  //跳转服务项目
  toserveItem(){
    if (this.data.province === '' || this.data.city === '') return util.toast('请先选择店铺位置信息')
    if (this.data.category_ids === '') return util.toast('请先选择店铺经营范围')

    util.skip('/service/serveItem/serveItem?category_ids=' + this.data.category_ids + '&goods_ids=' + this.data.goods_ids+'&city='+this.data.city, {
      selectGood: (data) => {
        this.setData({
          goodsNum: data.goods.length,
          goods_ids: data.goods.join(',')
        })
      }
    })
   
  },
  

  
  getResult() {
    http.post('shop/apply', {}, true, false).then(res => {
      if (res.data) {
        let arr = res.data.category_ids.split(',')
        arr = arr.map(value => {
          return Number(value)
        })
        this.setData({
          name: res.data.name,
          type: res.data.type,
          credit_code: res.data.credit_code,
          license_image: res.data.license_image,
          username: res.data.username,
          idcard: res.data.idcard,
          mobile: res.data.mobile,
          front_image: res.data.front_image,
          opposite_image: res.data.opposite_image,
          abbr: res.data.abbr,
          trade_hour: res.data.trade_hour,
          intro: res.data.intro,
          logo_image: res.data.logo_image,
          category_ids: res.data.category_ids,
          tags: arr,
          goods_ids: res.data.goods_ids,
          goodsNum: res.data.goods_ids.split(',').length,
          leader_name: res.data.leader_name,
          leader_mobile: res.data.leader_mobile,
          province: res.data.province,
          city: res.data.city,
          district: res.data.district,
          address: res.data.address,
          lng: res.data.lng,
          lat: res.data.lat,
          info: res.data.province + res.data.city + res.data.district,
          state: res.data.state,
          note: res.data.note
        })
      } else {
        util.skip('/service/enter/enter')
      }
    })
  },
  getInfo() {
    http.post('category/getlist', '', true).then(res => {
      this.setData({
        cate: res.data
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`
    })
    this.getResult()
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})